﻿''' <summary>
''' 凭证类别数据操作类，对应实例ClsVoucherGroup
''' </summary>
Public Class ClsTVoucherGroupDbAccess
    Inherits ClsBaseDbAccess

    Public Sub New(ByVal s As String)
        MyBase.New(s, TABLE_NAME_T_VOUCHER_GROUP)
    End Sub

    ''' <summary>
    ''' 两个参数，s - 连接字串；n - 表名
    ''' </summary>
    ''' <param name="s"></param>
    ''' <param name="n"></param>
    Public Sub New(ByVal s As String, ByVal n As String)
        MyBase.New(s, n)
    End Sub

    Public Function DeleteVoucherGroup(ByVal tmpCls As ClsTVoucherGroup) As Integer
        Dim tmpReturn As Integer = 0

        Try

            Dim tmpSql As String
            Dim tmpDbAccess = GetConnection()

            Dim tmpParas(0) As OleDb.OleDbParameter

            '打开数据库
            tmpDbAccess.DbOpen()
            '构造查询语句
            tmpSql = "Delete From [" & Me.AppTableName & "] "
            tmpSql &= " Where [GroupId] = @GroupId "


            '构造参数集合
            tmpParas(0) = New OleDb.OleDbParameter("@GroupId", OleDb.OleDbType.Integer)

            '为查询参数赋值
            tmpParas(0).Value = tmpCls.GroupId

            '执行更新查询(插入、修改、删除)
            tmpReturn = tmpDbAccess.UpdateRecord(tmpSql, tmpParas)

            '关闭数据库
            tmpDbAccess.DbClose()

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsTVoucherGroupDbAccess - InsertGroup"

        End Try

        Return tmpReturn

    End Function


    Public Function SaveGroup(ByVal tmpCls As ClsTVoucherGroup) As Integer
        Try

            Dim tmpSql As String
            Dim tmpDbAccess = GetConnection()

            Dim tmpParas(2) As OleDb.OleDbParameter
            Dim tmpReturn As Integer

            '打开数据库
            tmpDbAccess.DbOpen()
            '构造查询语句
            tmpSql = "update [" & Me.AppTableName & "] "
            'tmpSql &= "[GroupId],[GroupName],[ShortName]"
            tmpSql &= "set [GroupName] =@GroupName,[ShortName] = @ShortName"
            tmpSql &= " Where [GroupId] = "
            tmpSql &= "@GroupId"


            '构造参数集合
            tmpParas(0) = New OleDb.OleDbParameter("@GroupName", OleDb.OleDbType.VarWChar)
            tmpParas(1) = New OleDb.OleDbParameter("@ShorName", OleDb.OleDbType.VarWChar)
            tmpParas(2) = New OleDb.OleDbParameter("@GroupId", OleDb.OleDbType.Integer)

            '为查询参数赋值
            tmpParas(0).Value = tmpCls.GroupName
            tmpParas(1).Value = tmpCls.ShortName
            tmpParas(2).Value = tmpCls.GroupId

            '执行更新查询(插入、修改、删除)
            tmpReturn = tmpDbAccess.UpdateRecord(tmpSql, tmpParas)

            '关闭数据库
            tmpDbAccess.DbClose()

            Return tmpReturn

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsTVoucherGroupDbAccess - InsertGroup"

            Return 0
        End Try
    End Function

    Public Function InsertGroup(ByVal tmpCls As ClsTVoucherGroup) As Integer
        Try

            Dim tmpSql As String
            Dim tmpDbAccess = GetConnection()

            Dim tmpParas(1) As OleDb.OleDbParameter
            Dim tmpReturn As Integer

            '打开数据库
            tmpDbAccess.DbOpen()
            '构造查询语句
            tmpSql = "insert into [" & Me.AppTableName & "] ("
            'tmpSql &= "[GroupId],[GroupName],[ShortName]"
            tmpSql &= "[GroupName],[ShortName]"
            tmpSql &= ") values("
            tmpSql &= "@GroupName,@ShortName"
            tmpSql &= ")"

            '构造参数集合
            'tmpParas(0) = New OleDb.OleDbParameter("@GroupId", OleDb.OleDbType.Integer)
            tmpParas(0) = New OleDb.OleDbParameter("@GroupName", OleDb.OleDbType.VarWChar)
            tmpParas(1) = New OleDb.OleDbParameter("@ShorName", OleDb.OleDbType.VarWChar)

            '为查询参数赋值
            tmpParas(0).Value = tmpCls.GroupName
            tmpParas(1).Value = tmpCls.ShortName

            '执行更新查询(插入、修改、删除)
            tmpReturn = tmpDbAccess.InsertData(tmpSql, tmpParas, True) 'tmpDbAccess.UpdateRecord(tmpSql, tmpParas)

            '关闭数据库
            tmpDbAccess.DbClose()

            Return tmpReturn

        Catch ex As OleDb.OleDbException

            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "ClsTVoucherGroupDbAccess - InsertGroup"

            Return 0
        End Try
    End Function

    ''' <summary>
    ''' 返回查询结果集列表List(Of ClsTVoucherGroup)
    ''' </summary>
    ''' <param name="reader"></param>
    ''' <returns></returns>
    Public Overrides Function ConvertRecordToClass(ByVal reader As OleDb.OleDbDataReader) As Object
        If reader Is Nothing Then
            Return Nothing
        End If

        Dim tmpList As List(Of ClsTVoucherGroup) = New List(Of ClsTVoucherGroup)
        Try

            Dim tmpCls As ClsTVoucherGroup

            While reader.Read()

                tmpCls = New ClsTVoucherGroup()

                With tmpCls
                    .GroupId = reader.GetInt32(0)
                    .GroupName = ConvertDbNullString(reader(1))
                    .ShortName = ConvertDbNullString(reader(2))
                End With

                tmpList.Add(tmpCls)

            End While


        Catch ex As Exception

            mErrorNumber = -1001
            mErrorMessage = ex.Message
            mErrorCreater = "ClsTVoucherGroupDbAccess - ConvertRecordToClass"

            'Return Nothing
        End Try

        Return tmpList

    End Function

End Class
